import importlib
import logging
from parsers.base_parser import BaseParser

logger = logging.getLogger(__name__)

def dispatch_parser(bank_code, file_path):
    """根据银行代码分发到对应解析器"""
    try:
        # 动态导入解析器模块
        module_name = f"parsers.parser_{bank_code.lower()}"
        parser_module = importlib.import_module(module_name)
        parser_class = getattr(parser_module, f"Parser{bank_code.upper()}")
        if not issubclass(parser_class, BaseParser):
            raise ValueError("解析器未继承BaseParser")
        
        parser = parser_class()
        return parser.parse(file_path)
    except (ImportError, AttributeError) as e:
        logger.error(f"无法找到或加载银行 {bank_code} 的解析器: {e}")
        raise